package ide.ccf.ccproject.editors;

import ide.ccf.ccproject.common.EditorInputWatcher;
import ide.ccf.ccproject.common.IEditor;
import ide.ccf.ccproject.pages.ProjectFileCommonPage;
import ide.ccf.ccproject.pages.ProjectFileSourcePage;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;

/**
 * CCF项目文件编辑器
 *
 */
public class ProjectFileEditor extends FormEditor implements IEditor 
{
	public static String ID = "ide.ccf.ccproject.editors.ProjectFileEditor";
	
	private ProjectFileSourcePage sourceEditorPage = null;
	private ProjectFileCommonPage visualEditorPage = null;
	private EditorInputWatcher listener;
	private ColorManager colorManager;
	
	/**
	 * 构造器
	 */
	public ProjectFileEditor() 
	{
		super();
		
		colorManager = new ColorManager();
	}
	
	/**
	 * 初始化
	 */
	public void init(IEditorSite site, IEditorInput input) throws PartInitException 
	{
		super.init(site, input);
		listener = new EditorInputWatcher(this);
	}

	/**
	 * 保存
	 */
	public void doSave(IProgressMonitor monitor)
	{
		visualEditorPage.doSave(monitor);
		listener.updateTimeStamp();
	}
	
	/**
	 * 另存为
	 */
	public void doSaveAs()
	{
	
	}
	
	/**
	 * 是否允许另存为
	 */
	public boolean isSaveAsAllowed()
	{
		return false;
	}
	
	/**
	 * 是否可用
	 * @param actId
	 * @return
	 */
	public boolean isActionEnabled(int actId) 
	{
		return visualEditorPage.isActionEnabled(actId);
	}
	
	/**
	 * 切换
	 * @param actId
	 */
	public void triggerAction(int actId) 
	{
		visualEditorPage.triggerAction(actId);
	}
	
	/**
	 * 添加页面
	 */
	protected void addPages()
	{
		try 
		{
			visualEditorPage = new ProjectFileCommonPage(this);
			addPage(visualEditorPage);
			
			sourceEditorPage = new ProjectFileSourcePage(this);
			addPage(sourceEditorPage, getEditorInput());
		} 
		catch(Exception e) 
		{
			
		}
		setPartName(getEditorInput().getName());
	}
	
	/**
	 * 数据是否过时
	 */
	public boolean isDirty() 
	{
		if(visualEditorPage == null)
			visualEditorPage = new ProjectFileCommonPage(this);
		return visualEditorPage.isDirty();
	}
	
	/**
	 * 切换页面
	 */
	protected void pageChange(int newPageIndex) 
	{
		if (newPageIndex == 0) 
		{
			visualEditorPage.setFocus();
		} 
		else if (newPageIndex == 1) 
		{
			sourceEditorPage.getDocument().set(visualEditorPage.contents());
		}	
		super.pageChange(newPageIndex);
	}

	/**
	 * 释放
	 */
	public void dispose() 
	{
		listener.dispose();
		colorManager.dispose();
		
		super.dispose();
	}

	/**
	 * 重载
	 */
	public void reload() 
	{
		visualEditorPage.reload();
	}

	/**
	 * 设置焦点
	 */
	public void setFocus() 
	{
		super.setFocus();
		
		if (getCurrentPage() == 0)
			visualEditorPage.setFocus();
	}
	
	/**
	 * 返回ColorManager实例
	 * @return
	 */
	public ColorManager getColorManager()
	{
		return colorManager;
	}
	
	/**
	 * 获取编辑器源页面
	 */
	public ProjectFileSourcePage getSourceEditor()
	{
		return sourceEditorPage;
	}
	
	/**
	 * 获取编辑器可视化页面
	 */
	public ProjectFileCommonPage getVisualEditor()
	{
		return visualEditorPage;
	}
}